#!/usr/bin/perl
## Web::Scraper, RSSLite
## Web::Scraper - Web Scraping Toolkit inspired by Scrapi
use strict;
use warnings;
use URI;
use Web::Scraper;
use XML::RSS;
use CGI;
use Cache::File;
use YAML qw( LoadFile );
use Data::Dumper;

my $cgi = CGI->new(); $cgi->charset('utf-8');
my $rss = new XML::RSS(version => '2.0', encoding=>"utf-8");

my $pid = ( $cgi->param('pid') or 'soccerline' );
my $rss_id = LoadFile('Conf/rss_list.yaml') or die;

defined $rss_id->{$pid} or die;
#print Dumper( $rss_id ); exit;

my $url			= $rss_id->{$pid}->{url};
my $link_dsl	= $rss_id->{$pid}->{dsl};
my $link_reg	= $rss_id->{$pid}->{reg};

## Tie Cache::File
my %cached_rss;
tie %cached_rss, 'Cache::File', { cache_root => './web2rss', default_expires => '10 sec' };
##print defined $cached_rss{$pid}; exit;

## 캐슁된 RSS가 있다면 보여 주고 끝. 
if ( defined $cached_rss{$pid} ) {
	print $cgi->header("text/xml"), $cached_rss{$pid};
	exit;
}

## Web::Scraper
my $uri = URI->new($url);
my $scrp = scraper {
	process "title", "title" => 'TEXT'; 
    process $link_dsl, 
    "items[]" => {
    	'title' => 'TEXT',
    	'link' => '@href',
    };
};

my $res = $scrp->scrape( $uri );
#print Dumper ( $res ); exit;

## RSS만들기 
$rss->channel(
	title        => $res->{title},
	link         => $url,
	description  => sprintf( "RSS Feed for %s ( %s )", $url, scalar(localtime) ) ,
);

foreach my $item ( @{$res->{items}} ) {
	if ( $item->{link} =~ m!$link_reg!i ) {
		$rss->add_item(
			title => $item->{title},
			link => $item->{link},
			guid => $item->{link},
		);
	}
}

## 내용을 Cache 함 
$cached_rss{$pid} = $rss->as_string;

## RSS Feed 출력
print $cgi->header("text/xml"), $rss->as_string;

